﻿using System;
using System.Linq;
using System.Windows.Forms;
using TimberSmart.Business.Contracts;
using TimberSmart.Model.Security;

namespace TimberSmart.Desktop
{
    public partial class CustomerForm : Form
    {
        private IUserService _userService;
        private BindingSourceTracker<User> _tracker;

        public CustomerForm(IUserService customerService)
        {
            this._userService = customerService;
            InitializeComponent();
            _tracker = new BindingSourceTracker<User>(customerBindingSource);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Access the database and bring down the results.
            //var customers = this._userService.FindAllUsers(null).ToList();
            //customerBindingSource.DataSource = customers;            
            //customerBindingSource.BindingComplete += customerBindingSource_BindingComplete;
        }

        void customerBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            toolStripLabel1.Text = "Add " + _tracker.GetAdded().Count().ToString();
            toolStripLabel2.Text = "Update " + _tracker.GetUpdated().Count().ToString();
            toolStripLabel3.Text = "Delete " + _tracker.GetDeleted().Count().ToString();
         }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //foreach (var added in _tracker.GetAdded())
            //{
            //    _userService.AddCustomer(added);
            //}

            //foreach (var updated in _tracker.GetUpdated())
            //{
            //    _userService.UpdateCustomer(updated);
            //}

            //foreach (var deleted in _tracker.GetDeleted())
            //{
            //    _userService.DeleteCustomer(deleted);
            //}

            //_userService.Save();

            //_tracker.Dispose();
            //var customers = this._userService.GetCustomers().ToList();
            //customerBindingSource.DataSource = customers;  
            //_tracker = new BindingSourceTracker<Customer>(customerBindingSource);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //_tracker.Dispose();
            //var customers = this._userService.GetCustomers().ToList();
            //customerBindingSource.DataSource = customers;  
            //_tracker = new BindingSourceTracker<Customer>(customerBindingSource);
        }

        private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            //button1_Click(null, null);
        }
    }
}
